import fs from 'fs'

import PROJECT from '../PROJECT'

const html = fs.readFileSync(PROJECT.BUILD_CLIENT.HTML)

const render = ({
  content = '',
  state,
  entry,
  extractor,
}) => {
  const fullHtml = html.toString()
    .replace("'__DATA__'", JSON.stringify(state))
    .replace('%root%', content)
    .replace('%title%', entry ? entry.title(state) : '')
    .replace('%keywords%', entry ? entry.keywords(state) : '')
    .replace('%description%', entry ? entry.description(state) : '')
    .replace('</head>', `${extractor.getStyleTags()}\n</head>`)
    .replace('</body>', `${extractor.getScriptTags()}\n</body>`)

  return fullHtml
}

export default render